package com.zfx.service.impl;

import com.zfx.mapper.IContractRemindMapper;
import com.zfx.mapper.ILevel1SystemMapper;
import com.zfx.pojo.ContractRemind;
import com.zfx.service.IContractRemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: zfx
 * @version: 1.0
 * @time: 2020-05-22 13:30
 * <p>
 * 修改历史：
 * 修改日期      修改人员    版本    内容
 * 2020-05-22   zfx       1.0
 */
@Service
public class ContractRemindServiceImpl implements IContractRemindService {

    private static final int NOT_REMIND_NUM = 0;
    private static final int REMIND_NUM = 1;

    @Autowired
    private IContractRemindMapper contractRemindMapper;

    @Autowired
    private ILevel1SystemMapper level1SystemMapper;

    @Transactional(readOnly = true)
    @Override
    public List<ContractRemind> findRemindByRemindId(int remindId) {
        System.out.println(contractRemindMapper.findRemindByRemindId(remindId));
        return contractRemindMapper.findRemindByRemindId(remindId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateByRead(int remindId) {

        int num = 0;

        ContractRemind contractRemind = new ContractRemind();
        contractRemind.setRemindId(remindId);

        List<ContractRemind> remindList = contractRemindMapper.select(contractRemind);

        for ( ContractRemind remind : remindList) {
            remind.setState(NOT_REMIND_NUM);
            num += contractRemindMapper.updateByPrimaryKeySelective(remind);
        }

        return num;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateByRemind(int tableId) {

        String state = level1SystemMapper.selectByPrimaryKey(tableId).getState();
        String[] splits = state.split("-");

        String sRemindId = splits[splits.length-1].split("_")[0];

        ContractRemind remind = new ContractRemind();
        remind.setRemindId(Integer.valueOf(sRemindId));
        remind.setTableId(tableId);
        remind.setState(REMIND_NUM);

        return contractRemindMapper.updateByPrimaryKeySelective(remind);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteByTableId(int tableId) {
        return contractRemindMapper.deleteByPrimaryKey(tableId);
    }
}
